package com.mall.controller;

import com.mall.domain.Member;
import com.mall.domain.Order;
import com.mall.domain.Page;
import com.mall.service.impl.OrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单web层
 * @author : liangY
 * @date : 2021-01-12 23:36
 */
@Controller
@RequestMapping("orders")
public class OrderController {

    @Autowired
    private OrderServiceImpl orderService;

    /**
     * 查询所有订单
     * @param page
     * @return
     */
    @RequestMapping("/findAll")
    public @ResponseBody
    Map<String, Object> findAll(@RequestBody Page page){
        Map<String,Object> orderMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //获取订单集合
        List<Order> orders = orderService.findAll(page);
        //放入dataMap集合
        dataMap.put("orders",orders);
        dataMap.put("total",orderService.findTotal());
        //放入Map集合
        orderMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("msg","获取订单列表成功");
        metaMap.put("status",200);

        orderMap.put("meta",metaMap);
        return orderMap;
    }

    /**
     * 更新指定订单
     * @param order
     * @return
     */
    @RequestMapping("/update")
    public @ResponseBody
    Map<String, Object> update(@RequestBody Order order){
        Map<String,Object> orderMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入dataMap集合
        dataMap.put("order",order);
        //放入Map集合
        orderMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = orderService.update(order);
        if(flag > 0){
            metaMap.put("msg","更新订单成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","更新订单失败");
            metaMap.put("status",0);
        }
        orderMap.put("meta",metaMap);
        return orderMap;
    }

    /**
     * 添加订单
     * @param order
     * @return
     */
    @RequestMapping("/add")
    public @ResponseBody
    Map<String, Object> add(@RequestBody Order order){
        Map<String,Object> orderMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入dataMap集合
        dataMap.put("order",order);
        //放入Map集合
        orderMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = orderService.add(order);
        if(flag > 0){
            metaMap.put("msg","添加订单成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","更新订单失败");
            metaMap.put("status",0);
        }
        orderMap.put("meta",metaMap);
        return orderMap;
    }

    /**
     * 删除订单
     * @param id
     * @return
     */
    @RequestMapping("/del")
    public @ResponseBody
    Map<String, Object> add(Integer id){
        Map<String,Object> orderMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入Map集合
        orderMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = orderService.del(id);
        if(flag > 0){
            metaMap.put("msg","删除订单成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","删除订单失败");
            metaMap.put("status",0);
        }
        orderMap.put("meta",metaMap);
        return orderMap;
    }
}
